#include "signaldata.h"


SignalData::SignalData()
{
    d_flag = true;
}

SignalData::~SignalData()
{
    qDebug() << "~SignalData()";
    clear();
}

void SignalData::clear()
{
    //очистить вектор d_vectorData
   int i =0;
   while(i<d_vectorData.size())
   {
       qDebug()<<"d_vectorData.size()" << d_vectorData.size();
       d_vectorData.removeLast();
   }
}

int SignalData::size(int index) const
{
    PrivateData *data = d_vectorData.at(index);
//    qDebug() << "PrivateData.at("<<index<<") = " << data->m_values.size();
    return data->m_values.size();
}

QPointF SignalData::value(int index, int i) const
{
    return d_vectorData.at(index)->m_values.at(i);
}

QRectF SignalData::boundingRect(int index) const
{
    return d_vectorData.at(index)->m_boundingRect;
}

void SignalData::lock(int index)
{
    d_vectorData.at(index)->m_lock.lockForRead();
}

void SignalData::unlock(int index)
{
    d_vectorData.at(index)->m_lock.unlock();
}

void SignalData::setPath(const QString &path)
{
    d_path = path;
}

void SignalData::setCountParam(const int countParam)
{
    d_countParam = countParam;
}

void SignalData::initData()
{
    clear();
    qDebug()<<"initData";
    qDebug()<<"d_countParam:"<<d_countParam;
    qDebug()<<"1) d_vectorData.size(): "<<d_vectorData.size();
    for(int i = 0; i<d_countParam; i++)
    {
        d_vectorData.push_back(new PrivateData(i));
    }
    qDebug()<<"2) d_vectorData.size(): "<<d_vectorData.size();
}

void SignalData::setNumConnection(int numConnection)
{
    d_numConnection=numConnection;
}

SignalData &SignalData::instance()
{
    static SignalData valueVector;
    return valueVector;
}

void SignalData::append(const int index, const QPointF &point)
{
    PrivateData *data = d_vectorData.at(index);
    data->m_mutex.lock();
    data->m_pendingValues += point;

    const bool isLocked = data->m_lock.tryLockForWrite();
    if(isLocked)
    {
        const int numValues = data->m_pendingValues.size();
        const QPointF *pendingValue = data->m_pendingValues.data();

        for(int i = 0 ; i < numValues ; i++)
        {
            data->append(pendingValue[i]);
        }
        data->m_pendingValues.clear();
        data->m_lock.unlock();
    }
    data->m_mutex.unlock();
        qDebug() << " PrivateData : " << data->m_values.size();
}

void SignalData::append(double elapsed)
{

    qDebug()<<"SignalData::append()";
    qDebug()<<"d_numConnection: " <<d_numConnection;
    qDebug()<<"d_path: "<<d_path;
    qDebug()<<"d_countParam: "<<d_countParam;
    Isi *isi = new Isi(d_numConnection);
    isi->setPath(d_path);
    if(!isi->open())
    {
        delete isi;
        return;
    }
    qDebug() << "COUNTPARAM: "<< isi->getParams();
//    int row = 0;
    quint64 time;
    quint64 value;
    for(int i=1; i<=d_countParam;i++)
    {
        int row = isi->selectRow(elapsed);
        if(row!=0)
        {
        time = isi->selectTime(row,TimeIsi::time_sev);      ///!!!!
        qDebug() << " при i = " <<i ;
        value = (i-1) * 2 + (isi->selectValue(row,i));
        QPointF point =  QPointF(time,value);  //точка из Isi
        append(i-1,point);
        }
        else{
            d_flag = false;
            delete isi;
            return;
        }
    }

    delete isi;
    return;
}

void SignalData::clearStaleValues(double min, int index)
{
    PrivateData *data = d_vectorData.at(index);
    data->m_lock.lockForWrite();
    data->m_boundingRect = QRectF(1.0, 1.0, -2.0, -2.0);

    const QVector<QPointF> values = data->m_values;
    data->m_values.clear();
    data->m_values.reserve(values.size());

    int count = values.size();
    int i;
    for(i = count-1 ; i >= 0 ; i--)
    {
        if(values.at(i).x() < min) break;
    }
    if(i > 0)
    {
        data->m_values.append(values.at(i++));
    }
    while(i < count -1)
    {
        data->m_values.append(values.at(++i));
    }
    data->m_lock.unlock();
}

QString SignalData::getNameParam(int index)
{
    qDebug() << " ----";
    Isi *isi = new Isi(d_numConnection);
    isi->setPath(d_path);
    if(!isi->open())
    {
        delete isi;
    }
    QString name = isi->getNameParam(index+1);
    delete isi;
    return name;
}

bool SignalData::getFlag()
{
    return d_flag;
}

void SignalData::setFlag(bool flag)
{
    d_flag = flag;
}
